package com.xiaomaoguai.jdk.semaphore;

import org.junit.Test;

import java.util.concurrent.Semaphore;

/**
 * @Description :semaphore信号量-->http://ifeve.com/semaphore/
 * @FileName: SemaphoreTest.java
 * @Author :WeiHui.Zhang
 * @Data : 2015年11月4日 下午3:19:47
 * @Version:V1.00
 */
public class SemaphoreTest {

	/**
	 * <p>
	 * semaphore信号量--简单的Semaphore实现
	 * </p>
	 * 1.使用Semaphore来发出信号</br>
	 * 2. 可计数的Semaphore</br>
	 * 3. 有上限的Semaphore></br>
	 * 4.把Semaphore当锁来使用</br>
	 */

	/**
	 * 发出信号
	 */
	@Test
	public void signal() {
		//Semaphore semaphore = new Semaphore(2);

		//ReceivingThread receiver = new ReceivingThread(semaphore);
		//SendingThread sender = new SendingThread(semaphore);
	}

	class SendingThread extends Thread {

		private Semaphore semaphore;

		SendingThread(Semaphore semaphore) {
			this.semaphore = semaphore;
		}

		@Override
		public void run() {

			try {
				// 发出一个信号
				semaphore.acquire();
			} catch (InterruptedException e) {
				e.printStackTrace();
			}

		}
	}

	class ReceivingThread extends Thread {

		private Semaphore semaphore;

		ReceivingThread(Semaphore semaphore) {
			this.semaphore = semaphore;
		}

		@Override
		public void run() {

			semaphore.release();
		}
	}
}
